import { User} from "@/types/proto/api/v1/user_service";
import { SignInWithSSORequest} from "@/types/proto/api/v1/auth_service";
import { SignUpRequest, SignInRequest} from "@/types/proto/api/v1/auth_service";
import { authServiceClient} from "./grpcweb";
import {Metadata } from 'nice-grpc-web';
import { Empty } from "@/types/proto/google/protobuf/empty"; // Change this line
import { SignOutRequest, GetAuthStatusRequest} from "@/types/proto/api/v1/auth_service";
import {useUserStore} from "@/store/user"

export interface UserAuth {
  token: string;
  user: User;  // 根据你的用户类型定义具体类型
}

export async function SignInWithSSO(data: SignInWithSSORequest): Promise<UserAuth> {
  try {  
    let tokenFromHeader="";

    const u = await authServiceClient.signInWithSSO(data,
      {
        onHeader: (header: Metadata) => {
            // 在这里处理header
            const authHeader = header.get('authorization');
            console.log('Authorization authHeader:', authHeader);

            tokenFromHeader = authHeader && authHeader.trim() !== '' 
                  ? authHeader 
                  : ''; 
            console.log('Authorization:', tokenFromHeader);
        }
    })
    return {
      token: tokenFromHeader,
      user: u,  
    };
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}



export async function SignIn(data: SignInRequest): Promise<UserAuth> {
  try {  
    let tokenFromHeader="";

    const u = await authServiceClient.signIn(data,
      {
        onHeader: (header: Metadata) => {
            // 在这里处理header
            const authHeader = header.get('authorization');
            console.log('Authorization authHeader:', authHeader);

            tokenFromHeader = authHeader && authHeader.trim() !== '' 
                  ? authHeader 
                  : ''; 
            console.log('Authorization:', tokenFromHeader);
        }
    })
    return {
      token: tokenFromHeader,
      user: u,  // 假设response中包含用户信息
    };
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function SignUp(data: SignUpRequest): Promise<UserAuth> {
  try {  
    let tokenFromHeader="";

    const u = await authServiceClient.signUp(data,
      {
        onHeader: (header: Metadata) => {
            const authHeader = header.get('authorization');
            console.log('Authorization authHeader:', authHeader);

            tokenFromHeader = authHeader && authHeader.trim() !== '' 
                  ? authHeader 
                  : ''; 
            console.log('Authorization:', tokenFromHeader);
        }
    })
    return {
      token: tokenFromHeader,
      user: u,  
    };
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}

export async function SignOut(data: SignOutRequest): Promise<Empty> {
  try {  
    const store = useUserStore();
    return authServiceClient.signOut(data,{metadata:store.getHeaderToken()})
  } catch (error) {
    console.error('gRPC request failed:', error);
    throw error;
  }
}
